﻿/* 
     Class description : Load and create own made models into the scene
     ------------------------------------------------------------------
     Base from Komires and modified by William Beltrame 
 */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Komires.MataliPhysics;

namespace MySimpleScene
{
    public class UserShapes
    {
        Game1 demo;
        PhysicsScene scene;

        public UserShapes(Game1 demo)
        {
            this.demo = demo;
        }

        public void Initialize(PhysicsScene scene)
        {
            this.scene = scene;
        }

        // Create new shapes to add to the scene
        public static void CreateShapes(Game1 demo, PhysicsScene scene)
        {
            Shape shape = null;
            ShapePrimitive shapePrimitive = null;

            Shape box    = scene.Factory.ShapeManager.Find("Box");
            Shape sphere = scene.Factory.ShapeManager.Find("Sphere");
            Shape blabla = scene.Factory.ShapeManager.Find("blabla");
            Shape basketStruct = scene.Factory.ShapeManager.Find("BasketStruct");
         
            // Small Box
            shape = scene.Factory.ShapeManager.Create("UserShape 1");
            shape.Add(box, Matrix.Identity, 0.0f, ShapeCompoundType.MinkowskiSum);

            if (!demo.Meshes.ContainsKey("UserShape1"))
                demo.Meshes.Add("UserShape1", new DemoMesh(demo, shape, demo.Textures["Default"], Vector2.One, false, false, true, RasterizerState.CullClockwise));

            // Basketball object
            shape = scene.Factory.ShapeManager.Create("BasketBall");
            shape.Add(sphere, Matrix.Identity, 0.0f, ShapeCompoundType.MinkowskiSum);

            if (!demo.Meshes.ContainsKey("BasketBall"))
                demo.Meshes.Add("BasketBall", new DemoMesh(demo, shape, demo.Textures["BasketBall"], Vector2.One, false, false, true, RasterizerState.CullClockwise));

            // Basketball structure
            /*shape = scene.Factory.ShapeManager.Create("Basket_Struct");
            shape.Add(basketStruct, Matrix.Identity, 0.0f, ShapeCompoundType.MinkowskiSum);*/

            if (!demo.Meshes.ContainsKey("Basket_Struct"))
                demo.Meshes.Add("Basket_Struct", new DemoMesh(demo, shape, demo.Textures["Default"], Vector2.One, false, false, true, RasterizerState.CullClockwise));
        }

        // Create and add the shapes to the actual scene
        public void Create()
        {
            PhysicsObject objectBase = null;

            Shape MiniBox = scene.Factory.ShapeManager.Find("UserShape 1");
            Shape userShape2 = scene.Factory.ShapeManager.Find("BasketBall");
            Shape basketStruct = scene.Factory.ShapeManager.Find("BasketStruct");

            // Physics properties of the cube mesh
            objectBase = scene.Factory.PhysicsObjectManager.Create("UserShape 1");
            objectBase.Shape = MiniBox;
            objectBase.UserTagStr = "UserShape1";
            objectBase.InitLocalTransform.SetPosition(-20.0f, 20.0f, 23.0f);
            objectBase.InitLocalTransform.SetScale(2.0f);
            objectBase.Integral.SetDensity(1.0f);

            scene.UpdateFromInitLocalTransform(objectBase);

            // Physics properties of the bouncing basketball
            objectBase = scene.Factory.PhysicsObjectManager.Create("BasketBall");
            objectBase.Shape = userShape2;
            objectBase.UserTagStr = "BasketBall";
            objectBase.InitLocalTransform.SetPosition(10.0f, 30.0f, 20.0f);
            objectBase.InitLocalTransform.SetScale(2.0f);
            objectBase.Integral.SetDensity(1.0f);

            // Restitution = Manage the way the ball reacts when it touches the floor
            objectBase.Material.Restitution = 0.75f;

            scene.UpdateFromInitLocalTransform(objectBase);

            // Physics properties of the cube mesh
            objectBase = scene.Factory.PhysicsObjectManager.Create("Basket_Struct");
            objectBase.Shape = basketStruct;
            objectBase.UserTagStr = "Basket_Struct";
            objectBase.InitLocalTransform.SetPosition(-30.0f, 20.0f, 23.0f);
            objectBase.InitLocalTransform.SetScale(1.0f);
            objectBase.Integral.SetDensity(1.0f);

            scene.UpdateFromInitLocalTransform(objectBase);

        }
    }
}
